package com.zk.monitormanagement.utils;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.zk.monitormanagement.R;

import java.util.Timer;
import java.util.TimerTask;


/**
 * 加载的dialog
 */

public class DialogTools {
    private static Dialog mWaittingDialog;
    public static void showWaittingDialog(Context context){
        showWaittingDialog(context,"加载中...");
    }
    public static void showWaittingDialog(Context context, String msg) {
        try {
            if (mWaittingDialog != null) {
                mWaittingDialog.dismiss();
                mWaittingDialog = null;
            }
            LayoutInflater inflater = LayoutInflater.from(context);
            View v = inflater.inflate(R.layout.dialog_loading, null);// 得到加载view
            LinearLayout layout = (LinearLayout) v
                    .findViewById(R.id.dialog_loading_view);// 加载布局
            TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字
            tipTextView.setText(msg);// 设置加载信息

            mWaittingDialog = new Dialog(context, R.style.MyDialogStyle);// 创建自定义样式dialog
            mWaittingDialog.setCancelable(false); // 是否可以按“返回键”消失
            mWaittingDialog.setCanceledOnTouchOutside(false); // 点击加载框以外的区域
            mWaittingDialog.setContentView(layout, new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
            /**
             *将显示Dialog的方法封装在这里面
             */
            Window window = mWaittingDialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setGravity(Gravity.CENTER);
            window.setAttributes(lp);
            window.setWindowAnimations(R.style.PopWindowAnimStyle);
            mWaittingDialog.show();
            // 3秒后还未完成任务，则设置为可取消
            TimerTask task = new TimerTask() {
                @Override
                public void run() {
                    if (mWaittingDialog != null)
                        mWaittingDialog.setCancelable(true);
                }
            };
            Timer timer = new Timer(true);
            timer.schedule(task, 3000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void closeWaittingDialog() {
        try {
            if (mWaittingDialog != null)
                mWaittingDialog.dismiss();
            mWaittingDialog = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static ProgressDialog mProgressDialog;
    public static void showProgressDialog(Context context, String msg) {
        try {
            if (mWaittingDialog != null) {
                mWaittingDialog.dismiss();
                mWaittingDialog = null;
            }
            mProgressDialog = new ProgressDialog(context, ProgressDialog.THEME_DEVICE_DEFAULT_LIGHT);
            mProgressDialog.setCanceledOnTouchOutside(false);
            mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    mProgressDialog.dismiss();
                }
            });
            mProgressDialog.setMessage(msg);
            mProgressDialog.show();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void closeProgressDialog() {
        try {
            if (mProgressDialog != null)
                mProgressDialog.dismiss();
            mProgressDialog = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
